{% assign asofdate = 'Global' | PageParameter:'AsofDate' | AsDateTime | Date:'MM/dd/yyyy' %}
{% assign periodoryear = 'Global' | PageParameter:'PeriodorYear' | AsBoolean %}
{% if asofdate and asofdate !='' %}
    {% assign thisweeksunday = asofdate | SundayDate | Date:'MM/dd/yyyy' %}
{% else %}
    {% assign thisweeksunday = 'Now' | SundayDate | Date:'MM/dd/yyyy' %}
{% endif %}
{% assign pwthisweeksunday = thisweeksunday | DateAdd:-7,'d' | Date:'MM/dd/yyyy' %}
{% assign start13 = thisweeksunday | DateAdd:-97,'d' | Date:'MM/dd/yyyy' %}
{% assign thismo = thisweeksunday | Date:'MM' %}
{% assign thisyr = thisweeksunday | Date:'yyyy' %}
{% assign pythisyr = thisyr | Minus:1 | Date:'yyyy' %}
{% assign fomo = thismo | Append:'/01/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
{% assign eomo = fomo | DateAdd:1,'M' | DateAdd:-1,'d' | AsDateTime | Date:'MM/dd/yyyy' %}
{% assign foyr = '01/01/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
{% assign eoyr = '12/31/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
{% assign pyfoyr = foyr | DateAdd:-1,'y' | Date:'MM/dd/yyyy' %}
{% assign pyeoyr = eoyr | DateAdd:-1,'y' | Date:'MM/dd/yyyy' %}
{% if thismo >= 1 and thismo <= 3 %}
    {% assign foqtr = '01/01/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign eoqtr = '03/31/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
{% elseif thismo >= 4 and thismo <= 6 %}
    {% assign foqtr = '04/01/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign eoqtr = '06/30/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
{% elseif thismo >= 7 and thismo <= 9 %}
    {% assign foqtr = '07/01/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign eoqtr = '09/30/' | Append:thisyr | AsDateTime %}
{% elseif thismo >= 10 and thismo <= 12 %}
    {% assign foqtr = '10/01/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign eoqtr = '12/31/' | Append:thisyr | AsDateTime | Date:'MM/dd/yyyy' %}
{% endif %}
{% if periodoryear == true %}
    {% assign pyfomo = fomo | DateAdd:-1,'M' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign pyeomo = pyfomo | DateAdd:1,'M' | DateAdd:-1,'d' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign pyfoqtr = foqtr | DateAdd:-3,'M' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign pyeoqtr = pyfoqtr | DateAdd:3,'M' | DateAdd:-1,'d' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign priorpy = 'Prior' %}
{% else %}
    {% assign pyfomo = fomo | DateAdd:-1,'y' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign pyeomo = pyfomo | DateAdd:1,'M' | DateAdd:-1,'d' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign pyfoqtr = foqtr | DateAdd:-1,'y' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign pyeoqtr = eoqtr | DateAdd:-1,'y' | AsDateTime | Date:'MM/dd/yyyy' %}
    {% assign priorpy = 'Prior Year' %}
{% endif %}

{% comment %}
This is the SECRET SAUCE line "assign metricids". Here you enter a comma seperated list that will display the metric KPIs / 13 week /QTR / Year over Year.
{% endcomment %}

{% assign metricids = '0,0' %}

{% assign metricslist = metricids | Split:',' %}
{% assign mmids = '(' | Append:metricids | Append:')' %}
{% assign campuses = 'Global' | Attribute:'ShortCodeMetricsCampuses','RawValue' | Split:',' %}
{% for cp in campuses %}
    {% campus where:'Guid == "{{ cp }}"' securityenabled:'false' %}
        {% if forloop.first == true %}
            {% assign campussql = campus.Id %}
        {% else %}
            {% assign campussql = campussql | Append:',' | Append:campus.Id %}
        {% endif %}
    {% endcampus %}
{% endfor %}
{% assign activecampus = 'Global' | PageParameter:'CampusId' %}
{% if activecampus and activecampus != '' %}
    {% assign campussqllist = '(' | Append:activecampus | Append:')' %}
{% else %}
    {% assign campussqllist = '(' | Append:campussql | Append:')' %}
{% endif %}
{% sql %}
   SELECT mv.[MetricId] AS [Metric]
        ,mvp.[EntityId] AS [Campus]
        ,ISNULL(FORMAT(mv.[MetricValueDateTime],'MM/dd/yyyy'),'') AS [TheDate]
        ,FORMAT(mv.[MetricValueDateTime],'MM') AS [Month]
        ,FORMAT(mv.[MetricValueDateTime],'dd') AS [Day]
        ,FORMAT(mv.[MetricValueDateTime],'yyyy') AS [Year]
        ,SUM(mv.[YValue]) AS [TheCount]
    FROM [dbo].[MetricValue] mv
    LEFT JOIN [MetricValuePartition] mvp on mvp.[MetricValueId] = mv.[Id]
    WHERE mv.[MetricId] IN {{ mmids }} AND mv.[MetricValueDateTime] >= '{{ foyr | DateAdd:-1,'y' | Date:'MM/dd/yyyy' }}' AND mv.[MetricValueDateTime] < '{{ foyr | DateAdd:1,'y' | Date:'MM/dd/yyyy' }}' AND mvp.[EntityId] IN {{ campussqllist }}
    GROUP BY mv.[MetricId], FORMAT(mv.[MetricValueDateTime],'MM'), FORMAT(mv.[MetricValueDateTime],'dd'), FORMAT(mv.[MetricValueDateTime],'yyyy'), FORMAT(mv.[MetricValueDateTime],'MM/dd/yyyy'), mvp.[EntityId]
    ORDER BY mv.[MetricId], FORMAT(mv.[MetricValueDateTime],'yyyy'), FORMAT(mv.[MetricValueDateTime],'MM'), FORMAT(mv.[MetricValueDateTime],'dd'), FORMAT(mv.[MetricValueDateTime],'MM/dd/yyyy'), mvp.[EntityId];
{% endsql %}

{% comment %}
The following variable assignments are used to set the necessary display items to null.
{% endcomment %}

{% assign campsct = campuses | Size %}
{% if campsct and campsct >= 1 %}
    {% if activecampus and activecampus != '' %}
        {% campus id:'{{ activecampus }}' %}
            {% assign cpshortcode = campus.ShortCode %}
            {% assign cpcolor = campus | Attribute:'CampusColor' %}
            {% if cpcolor and cpcolor != ''%}{% else %}{% assign cpcolor = 'gray' %}{% endif %}
            {% assign cpicon = campus | Attribute:'CampusIcon' %}
            {% if cpicon and cpicon != ''%}{% else %}{% assign cpicon = 'fa fa-church' %}{% endif %}
        {% endcampus %}
    {% else %}
        {% assign cpshortcode = 'All Campuses' %}
        {% assign cpcolor = 'gray' %}
        {% assign cpicon = 'fa fa-sitemap' %}
    {% endif %}
    {[ panel title:'{{ cpshortcode }}' icon:'{{ cpicon }}' type:'primary' ]}
    {% for displaymetric in metricslist %}
        {% metric id:'{{ displaymetric }}' securityenabled:'false' %}
            {% assign metricsubtitle = metric.Subtitle %}
            {% assign metricicon = metric.IconCssClass %}
            {% assign metriccumulativeavg = metric | Attribute:'CumulativeorAverage','RawValue' | AsBoolean %}
        {% endmetric %}

        {% if metriccumulativeavg == true %}
            {% assign cumavgtxt = 'AVG' %}
        {% else %}
            {% assign cumavgtxt = '' %}
        {% endif %}

        {% assign aodkpi = '' %}
        {% assign pwaodkpi = '' %}
        {% assign aodmokpi = '' %}
        {% assign aodmokpict = '' %}
        {% assign pyaodmokpi = '' %}
        {% assign pyaodmokpict = '' %}
        {% assign aodqtrkpi = '' %}
        {% assign aodqtrkpict = '' %}
        {% assign pyaodqtrkpi = '' %}
        {% assign pyaodqtrkpict = '' %}
        {% assign aodyrkpi = '' %}
        {% assign aodyrkpict = '' %}
        {% assign pyaodyrkpi = '' %}
        {% assign pyaodyrkpict = '' %}
        {% assign lastckdate = '' %}

        {% assign w13chartlbl = '' %}
        {% assign w13loopct = 1 %}
        {% assign w13linelbl = '' %}
        {% assign w13linect = '' %}
        
        {% assign cyqtrchartlbl = '' %}
        {% assign pyqtrchartlbl = '' %}
	    {% assign cyqtrloopct = 1 %}
        {% assign pyqtrloopct = 1 %}
        {% assign cyqtrlinelbl = '' %}
        {% assign cyqtrlinect = '' %}
	    {% assign pyqtrlinelbl = '' %}
        {% assign pyqtrlinect = '' %}

        {% assign cyyrchartlbl = '' %}
        {% assign pyyrchartlbl = '' %}
	    {% assign cyyrloopct = 1 %}
        {% assign pyyrloopct = 1 %}
        {% assign cyyrlinelbl = '' %}
        {% assign cyyrlinect = '' %}
	    {% assign pyyrlinelbl = '' %}
        {% assign pyyrlinect = '' %}
        {% assign cyyrcumavg = '' %}
        {% assign pyyrcumavg = '' %}
        
        {% for item in results %}
            {% if item.Metric == displaymetric %}
                {% assign ckdate = item.TheDate | AsDateTime | Date:'MM/dd/yyyy' %}

{% comment %}This is the lava to total up the items for displaying the KPIs.{% endcomment %}

                {% if ckdate == thisweeksunday %}
                    {% if aodkpi == '' %}
                        {% assign aodkpi = item.TheCount %}
                    {% else %}
                        {% assign aodkpi = aodkpi | Plus:item.TheCount %}
                    {% endif %}
                {% endif %}
                {% if ckdate == pwthisweeksunday %}
                    {% if  pwaodkpi == '' %}
                        {% assign pwaodkpi = item.TheCount %}
                    {% else %}
                        {% assign pwaodkpi = pwaodkpi | Plus:item.TheCount %}
                    {% endif %}
                {% endif %}
                {% assign cymo = ckdate | IsDateBetween:fomo,eomo %}
                {% if cymo == true %}
                    {% if  aodmokpi == '' %}
                        {% assign aodmokpi = item.TheCount %}
                    {% else %}
                        {% assign aodmokpi = aodmokpi | Plus:item.TheCount %}
                    {% endif %}
                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                        {% assign aodmokpict = aodmokpict | Plus:1 %}
                    {% endif %}
                {% endif %}
                {% assign pymo = ckdate | IsDateBetween:pyfomo,pyeomo %}
                {% if pymo == true %}
                    {% if  pyaodmokpi == '' %}
                        {% assign pyaodmokpi = item.TheCount %}
                    {% else %}
                        {% assign pyaodmokpi = pyaodmokpi | Plus:item.TheCount %}
                    {% endif %}
                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                        {% assign pyaodmokpict = pyaodmokpict | Plus:1 %}
                    {% endif %}
                {% endif %}
                {% assign cyqtr = ckdate | IsDateBetween:foqtr,eoqtr %}
                {% if cyqtr == true %}
                    {% if aodqtrkpi == '' %}
                        {% assign aodqtrkpi = item.TheCount %}
                    {% else %}
                        {% assign aodqtrkpi = aodqtrkpi | Plus:item.TheCount %}
                    {% endif %}
                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                        {% assign aodqtrkpict = aodqtrkpict | Plus:1 %}
                    {% endif %}
                {% endif %}
                {% assign pyqtr = ckdate | IsDateBetween:pyfoqtr,pyeoqtr %}
                {% if pyqtr == true %}
                    {% if pyaodqtrkpi == '' %}
                        {% assign pyaodqtrkpi = item.TheCount %}
                    {% else %}
                        {% assign pyaodqtrkpi = pyaodqtrkpi | Plus:item.TheCount %}
                    {% endif %}
                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                        {% assign pyaodqtrkpict = pyaodqtrkpict | Plus:1 %}
                    {% endif %}
                {% endif %}
                {% assign cyyr = ckdate | IsDateBetween:foyr,eoyr %}
                {% if cyyr == true %}
                    {% if aodyrkpi == '' %}
                        {% assign aodyrkpi = item.TheCount %}
                    {% else %}
                        {% assign aodyrkpi = aodyrkpi | Plus:item.TheCount %}
                    {% endif %}
                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                        {% assign aodyrkpict = aodyrkpict | Plus:1 %}
                    {% endif %}
                {% endif %}
                {% assign pyyr = ckdate | IsDateBetween:pyfoyr,pyeoyr %}
                {% if pyyr == true %}
                    {% if pyaodyrkpi == '' %}
                        {% assign pyaodyrkpi = item.TheCount %}
                    {% else %}
                        {% assign pyaodyrkpi = pyaodyrkpi | Plus:item.TheCount %}
                    {% endif %}
                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                        {% assign pyaodyrkpict = pyaodyrkpict | Plus:1 %}
                    {% endif %}
                {% endif %}

{% comment %}This is the lava to generate the labels and line for the 13week chart.{% endcomment %}

                {% assign wk13check = ckdate | IsDateBetween:start13,thisweeksunday %}
                {% if wk13check == true %}
                    {% assign moday = item.Month | Append:'/' | Append:item.Day %}
                    {% if w13loopct == 1 %}
                        {% assign w13linelbl = w13linelbl | Append:'Current week + Prior 13 weeks' %}
                        {% assign w13chartlbl = w13chartlbl | AddToArray:moday %}
                        {% assign w13linect = w13linect | AddToArray:item.TheCount %}
                        {% assign w13loopct = w13loopct | Plus:1 %}
                    {% else %}
                        {% assign w13foundit = 99 | AsInteger %}
                        {% for findmoday in w13chartlbl %}
                            {% if findmoday == moday %}{% assign w13foundit = forloop.index0 | AsInteger %}{% break %}{% endif %}
                        {% endfor %}
                        {% if w13foundit == 99 %}
                            {% assign w13chartlbl = w13chartlbl | AddToArray:moday %}
                            {% assign w13linect = w13linect | AddToArray:item.TheCount %}
                        {% else %}
                            {% assign w13linectnew = '' %}
                            {% for new13ct in w13linect %}
                                {% if forloop.index0 == w13foundit %}
                                    {% assign use13ct =  new13ct | Plus:item.TheCount %}
                                {% else %}
                                    {% assign use13ct = new13ct %}
                                {% endif %}
                                {% assign w13linectnew = w13linectnew | AddToArray:use13ct %}
                            {% endfor %}
                            {% assign w13linect = w13linectnew %}
                        {% endif %}
                    {% endif %}
                {% endif %}
                
{% comment %}This is the lava to generate the labels and lines for the QTR chart.{% endcomment %}

                {% assign qtrcheck = ckdate | IsDateBetween:foqtr,eoqtr %}
                {% if qtrcheck == true %}
                    {% assign cylbldate = item.TheDate | AsDateTime | Date:'MM/dd/yyyy' %}
                    {% if cyqtrloopct  == 1 %}
                        {% assign cyqtrlinelbl = cyqtrlinelbl | Append:foqtr | Date:'MM/dd/yyyy' | Append:' - ' | Append:eoqtr | Date:'MM/dd/yyyy' %}
                        {% assign cyqtrchartlbl = cyqtrchartlbl | AddToArray:cylbldate %}
                        {% assign cyqtrlinect = cyqtrlinect | AddToArray:item.TheCount %}
                        {% assign cyqtrloopct = cyqtrloopct | Plus:1 %}
                    {% else %}
                        {% assign cyqtrfoundit = 99 | AsInteger %}
                        {% for findcylbldate in cyqtrchartlbl %}
                            {% if findcylbldate == cylbldate %}{% assign cyqtrfoundit = forloop.index0 | AsInteger %}{% break %}{% endif %}
                        {% endfor %}
                        {% if cyqtrfoundit == 99 %}
                            {% assign cyqtrchartlbl = cyqtrchartlbl | AddToArray:cylbldate %}
                            {% assign cyqtrlinect = cyqtrlinect | AddToArray:item.TheCount %}
                        {% else %}
                            {% assign cyqtrlinectnew = '' %}
                            {% for newcyqtrct in cyqtrlinect %}
                                {% if forloop.index0 == cyqtrfoundit %}
                                    {% assign usecyqtrct = newcyqtrct | Plus:item.TheCount %}
                                {% else %}
                                    {% assign usecyqtrct = newcyqtrct %}
                                {% endif %}
                                {% assign cyqtrlinectnew = cyqtrlinectnew | AddToArray:usecyqtrct %}
                            {% endfor %}
                            {% assign cyqtrlinect = cyqtrlinectnew %}
                        {% endif %}
                    {% endif %}
                {% endif %}
                {% assign pyqtrcheck = ckdate | IsDateBetween:pyfoqtr,pyeoqtr %}
                {% if pyqtrcheck == true %}
                    {% assign pylbldate = item.TheDate | AsDateTime | Date:'MM/dd/yyyy' %}
                    {% if pyqtrloopct  == 1 %}
                        {% assign pyqtrlinelbl = pyqtrlinelbl | Append:pyfoqtr | Date:'MM/dd/yyyy' | Append:' - ' | Append:pyeoqtr | Date:'MM/dd/yyyy' %}
                        {% assign pyqtrchartlbl = pyqtrchartlbl | AddToArray:pylbldate %}
                        {% assign pyqtrlinect = pyqtrlinect | AddToArray:item.TheCount %}
                        {% assign pyqtrloopct = pyqtrloopct | Plus:1 %}
                    {% else %}
                        {% assign pyqtrfoundit = 99 | AsInteger %}
                        {% for findpylbldate in pyqtrchartlbl %}
                            {% if findpylbldate == pylbldate %}{% assign pyqtrfoundit = forloop.index0 | AsInteger %}{% break %}{% endif %}
                        {% endfor %}
                        {% if pyqtrfoundit == 99 %}
                            {% assign pyqtrchartlbl = pyqtrchartlbl | AddToArray:pylbldate %}
                            {% assign pyqtrlinect = pyqtrlinect | AddToArray:item.TheCount %}
                        {% else %}
                            {% assign pyqtrlinectnew = '' %}
                            {% for newpyqtrct in pyqtrlinect %}
                                {% if forloop.index0 == pyqtrfoundit %}
                                    {% assign usepyqtrct = newpyqtrct | Plus:item.TheCount %}
                                {% else %}
                                    {% assign usepyqtrct = newpyqtrct %}
                                {% endif %}
                                {% assign pyqtrlinectnew = pyqtrlinectnew | AddToArray:usepyqtrct %}
                            {% endfor %}
                            {% assign pyqtrlinect = pyqtrlinectnew %}
                        {% endif %}
                    {% endif %}
                {% endif %}

{% comment %}This is the lava to generate the labels and lines for the YEAR chart.{% endcomment %}

                {% assign yrcheck = ckdate | IsDateBetween:foyr,eoyr %}
                {% if yrcheck == true %}
                    {% assign yrlbldate = item.Month %}
                    {% if cyyrloopct  == 1 %}
                        {% assign cyyrlinelbl = cyyrlinelbl | Append:foyr | Date:'MM/dd/yyyy' | Append:' - ' | Append:eoyr | Date:'MM/dd/yyyy' %}
                        {% assign cyyrchartlbl = cyyrchartlbl | AddToArray:yrlbldate %}
                        {% assign cyyrlinect = cyyrlinect | AddToArray:item.TheCount %}
                        {% assign cyyrloopct = cyyrloopct | Plus:1 %}
                        {% assign cyyrcumavg = cyyrcumavg | AddToArray:1 %}
                    {% else %}
                        {% assign cyyrfoundit = 99 | AsInteger %}
                        {% for findcylbldate in cyyrchartlbl %}
                            {% if findcylbldate == yrlbldate %}{% assign cyyrfoundit = forloop.index0 | AsInteger %}{% break %}{% endif %}
                        {% endfor %}
                        {% if cyyrfoundit == 99 %}
                            {% assign cyyrchartlbl = cyyrchartlbl | AddToArray:yrlbldate %}
                            {% assign cyyrlinect = cyyrlinect | AddToArray:item.TheCount %}
                            {% assign cyyrcumavg = cyyrcumavg | AddToArray:1 %}
                        {% else %}
                            {% assign cyyrlinectnew = '' %}
                            {% assign cyyrcumavgnew = '' %}
                            {% for newcyyrct in cyyrlinect %}
                                {% if forloop.index0 == cyyrfoundit %}
                                    {% assign usecyyrct = newcyyrct | Plus:item.TheCount %}
                                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                                        {% assign usecyyrcumavg = cyyrcumavg[forloop.index0] | Plus:1 %}
                                    {% else %}
                                        {% assign usecyyrcumavg = cyyrcumavg[forloop.index0] %}
                                    {% endif %}
                                {% else %}
                                    {% assign usecyyrct = newcyyrct %}
                                    {% assign usecyyrcumavg = cyyrcumavg[forloop.index0] %}
                                {% endif %}
                                {% assign cyyrlinectnew = cyyrlinectnew | AddToArray:usecyyrct %}
                                {% assign cyyrcumavgnew = cyyrcumavgnew | AddToArray:usecyyrcumavg %}
                            {% endfor %}
                            {% assign cyyrlinect = cyyrlinectnew %}
                            {% assign cyyrcumavg = cyyrcumavgnew %}
                        {% endif %}
                    {% endif %}
                {% endif %}
                {% assign pyyrcheck = ckdate | IsDateBetween:pyfoyr,pyeoyr %}
                {% if pyyrcheck == true %}
                    {% assign yrlbldate = item.Month %}
                    {% if pyyrloopct  == 1 %}
                        {% assign pyyrlinelbl = pyyrlinelbl | Append:pyfoyr | Date:'MM/dd/yyyy' | Append:' - ' | Append:pyeoyr | Date:'MM/dd/yyyy' %}
                        {% assign pyyrchartlbl = pyyrchartlbl | AddToArray:yrlbldate %}
                        {% assign pyyrlinect = pyyrlinect | AddToArray:item.TheCount %}
                        {% assign pyyrloopct = pyyrloopct | Plus:1 %}
                        {% assign pyyrcumavg = pyyrcumavg | AddToArray:1 %}
                    {% else %}
                        {% assign pyyrfoundit = 99 | AsInteger %}
                        {% for findpylbldate in pyyrchartlbl %}
                            {% if findpylbldate == yrlbldate %}{% assign pyyrfoundit = forloop.index0 | AsInteger %}{% break %}{% endif %}
                        {% endfor %}
                        {% if pyyrfoundit == 99 %}
                            {% assign pyyrchartlbl = pyyrchartlbl | AddToArray:yrlbldate %}
                            {% assign pyyrlinect = pyyrlinect | AddToArray:item.TheCount %}
                            {% assign pyyrcumavg = pyyrcumavg | AddToArray:1 %}
                        {% else %}
                            {% assign pyyrlinectnew = '' %}
                            {% assign pyyrcumavgnew = '' %}
                            {% for newpyyrct in pyyrlinect %}
                                {% if forloop.index0 == pyyrfoundit %}
                                    {% assign usepyyrct = newpyyrct | Plus:item.TheCount %}
                                    {% if ckdate != lastckdate and metriccumulativeavg == true %}
                                        {% assign usepyyrcumavg = pyyrcumavg[forloop.index0] | Plus:1 %}
                                    {% else %}
                                        {% assign usepyyrcumavg = pyyrcumavg[forloop.index0] %}
                                    {% endif %}
                                {% else %}
                                    {% assign usepyyrct = newpyyrct %}
                                    {% assign usepyyrcumavg = pyyrcumavg[forloop.index0] %}
                                {% endif %}
                                {% assign pyyrlinectnew = pyyrlinectnew | AddToArray:usepyyrct %}
                                {% assign pyyrcumavgnew = pyyrcumavgnew | AddToArray:usepyyrcumavg %}
                            {% endfor %}
                            {% assign pyyrlinect = pyyrlinectnew %}
                            {% assign pyyrcumavg = pyyrcumavgnew %}
                        {% endif %}
                    {% endif %}
                {% endif %}

            {% endif %}
            {% if ckdate != lastckdate %}{% assign lastckdate = ckdate %}{% endif %}
        {% endfor %}
        <h4>{{ metricsubtitle }}</h4>
            <div class="row">
                <div class="col-md-3">
                    {[kpis columncount:'1' size:'sm']}
                        [[ kpi icon:'{{ metricicon }}' value:'{{ aodkpi | AsInteger }}' label:'Week {{ thisweeksunday | DateAdd:-6,'d' | Date:'MM/dd' }} - {{ thisweeksunday }}' color:'{{ cpcolor }}']][[ endkpi ]]
                        [[ kpi icon:'{{ metricicon }}' value:'{{ pwaodkpi | AsInteger }}' label:'Prior Week {{ thisweeksunday | DateAdd:-13,'d' | Date:'MM/dd' }} - {{ thisweeksunday | DateAdd:-7,'d' | Date:'MM/dd/yyyy' }}' color:'black']][[ endkpi ]]
                     {[endkpis]}
                </div>
                <div class="col-md-3">
                    {% if metriccumulativeavg == true %}
                        {% if aodmokpict <= 0 %}{% assign aodmokpict = 1 %}{% endif %}
                        {% if pyaodmokpict <= 0 %}{% assign pyaodmokpict = 1 %}{% endif %}
                        {% assign aodmokpi = aodmokpi | DividedBy:aodmokpict | Ceiling %}
                        {% assign pyaodmokpi = pyaodmokpi | DividedBy:pyaodmokpict | Ceiling %}
                    {% endif %}
                    {[kpis columncount:'1' size:'sm']}
                        [[ kpi icon:'{{ metricicon }}' value:'{{ aodmokpi | AsInteger }}' label:'Month {{ cumavgtxt }} {{ fomo }} - {{ eomo }}' color:'{{ cpcolor }}']][[ endkpi ]]
                        [[ kpi icon:'{{ metricicon }}' value:'{{ pyaodmokpi | AsInteger }}' label:'{{ priorpy }} Month {{ cumavgtxt }} {{ pyfomo }} - {{ pyeomo }} ' color:'black']][[ endkpi ]]
                    {[endkpis]}
                </div>
                <div class="col-md-3">
                    {% if metriccumulativeavg == true %}
                        {% if aodqtrkpict <= 0 %}{% assign aodqtrkpict = 1 %}{% endif %}
                        {% if pyaodqtrkpict <= 0 %}{% assign pyaodqtrkpict = 1 %}{% endif %}
                        {% assign aodqtrkpi = aodqtrkpi | DividedBy:aodqtrkpict | Ceiling %}
                        {% assign pyaodqtrkpi = pyaodqtrkpi | DividedBy:pyaodqtrkpict | Ceiling %}
                    {% endif %}
                    {[kpis columncount:'1' size:'sm']}
                        [[ kpi icon:'{{ metricicon }}' value:'{{ aodqtrkpi | AsInteger }}' label:'QTR {{ cumavgtxt }} {{ foqtr | Date:'MM/dd/yyyy' }} - {{ eoqtr | Date:'MM/dd/yyyy' }}' color:'{{ cpcolor }}']][[ endkpi ]]
                        [[ kpi icon:'{{ metricicon }}' value:'{{ pyaodqtrkpi | AsInteger }}' label:'{{ priorpy }} QTR {{ cumavgtxt }} {{ pyfoqtr | Date:'MM/dd/yyyy' }} - {{ pyeoqtr | Date:'MM/dd/yyyy' }} ' color:'black']][[ endkpi ]]
                    {[endkpis]}
                </div>
                <div class="col-md-3">
                    {% if metriccumulativeavg == true %}
                        {% if aodyrkpict <= 0 %}{% assign aodyrkpict = 1 %}{% endif %}
                        {% if pyaodyrkpict <= 0 %}{% assign pyaodyrkpict = 1 %}{% endif %}
                        {% assign aodyrkpi = aodyrkpi | DividedBy:aodyrkpict | Ceiling %}
                        {% assign pyaodyrkpi = pyaodyrkpi | DividedBy:pyaodyrkpict | Ceiling %}
                    {% endif %}
                    {[kpis columncount:'1' size:'sm']}
                        [[ kpi icon:'{{ metricicon }}' value:'{{ aodyrkpi | AsInteger }}' label:'Year {{ cumavgtxt }} {{ foyr | Date:'MM/dd/yyyy' }} - {{ eoyr | Date:'MM/dd/yyyy' }}' color:'{{ cpcolor }}']][[ endkpi ]]
                        [[ kpi icon:'{{ metricicon }}' value:'{{ pyaodyrkpi | AsInteger }}' label:'Prior Year {{ cumavgtxt }} {{ pyfoyr | Date:'MM/dd/yyyy' }} - {{ pyeoyr | Date:'MM/dd/yyyy' }} ' color:'black']][[ endkpi ]]
                    {[endkpis]}
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-md-4">
                    {[ chart type:'line' chartheight:'300px' labels:'{{ w13chartlbl | Join:',' }}' xaxistype:'linear' legendshow:'true' legendposition:'top' ]}
                        [[ dataset label:'{{ w13linelbl }}' data:'{{ w13linect | Join:',' }}' fillcolor:'{{ cpcolor }}' bordercolor:'{{ cpcolor }}' borderwidth:'3' pointradius:'4' pointbordercolor:'{{ cpcolor }}' pointcolor:'{{ cpcolor }}' pointhovercolor:'{{ cpcolor }}' ]] [[ enddataset ]]
                    {[ endchart ]}
                </div>
                {% assign pyctcount = pyqtrlinect | Size %}
                {% assign cyctcount = cyqtrlinect | Size %}
                {% if pyctcount > cyctcount %}
                    {% assign usecount = pyctcount %}
                {% else %}
                    {% assign usecount = cyctcount %}
                {% endif %}
                {% assign useqtrlbl = '' %}
                {% for i in (0..usecount) %}
                    {% assign upydate = pyqtrchartlbl[i] | Date:'MM/dd/yy' %}
                    {% assign ucydate = cyqtrchartlbl[i] | Date:'MM/dd/yy' %}
                    {% assign uselbl = ucydate | Append:' - ' | Append:upydate %}
                    {% assign useqtrlbl = useqtrlbl | AddToArray:uselbl %}
                {% endfor %}
                <div class="col-md-4">
                    {[ chart type:'line' chartheight:'300px' labels:'{{ useqtrlbl | Join:',' }}' xaxistype:'linear' legendshow:'true' legendposition:'top' ]}
                         [[ dataset label:'{{ pyqtrlinelbl }}' data:'{{ pyqtrlinect | Join:',' }}' fillcolor:'black' bordercolor:'black' borderwidth:'3' pointradius:'4' pointbordercolor:'black' pointcolor:'black' pointhovercolor:'black' ]] [[ enddataset ]]
                         [[ dataset label:'{{ cyqtrlinelbl }}' data:'{{ cyqtrlinect | Join:',' }}' fillcolor:'{{ cpcolor }}' bordercolor:'{{ cpcolor }}' borderwidth:'3' pointradius:'4' pointbordercolor:'{{ cpcolor }}' pointcolor:'{{ cpcolor }}' pointhovercolor:'{{ cpcolor }}' ]] [[ enddataset ]]
                    {[ endchart ]}
                </div>
                <div class="col-md-4">
                    {% if metriccumulativeavg == true %}
                        {% assign tempcyyrlinect = '' %}
                        {% for item in cyyrlinect %}
                            {% assign usetempcylinect = item | DividedBy:cyyrcumavg[forloop.index0] | Ceiling %}
                            {% assign tempcyyrlinect = tempcyyrlinect | AddToArray:usetempcylinect %}
                        {% endfor %}
                        {% assign cyyrlinect = tempcyyrlinect %}
                        {% assign temppyyrlinect = '' %}
                        {% for item in pyyrlinect %}
                            {% assign usetemppylinect = item | DividedBy:pyyrcumavg[forloop.index0] | Ceiling %}
                            {% assign temppyyrlinect = temppyyrlinect | AddToArray:usetemppylinect %}
                        {% endfor %}
                        {% assign pyyrlinect = temppyyrlinect %}

                    {% endif %}
                    {[ chart type:'line' chartheight:'300px' labels:'{{ pyyrchartlbl | Join:',' }}' xaxistype:'linear' legendshow:'true' legendposition:'top' ]}
                         [[ dataset label:'{{ pyyrlinelbl }}' data:'{{ pyyrlinect | Join:',' }}' fillcolor:'black' bordercolor:'black' borderwidth:'3' pointradius:'4' pointbordercolor:'black' pointcolor:'black' pointhovercolor:'black' ]] [[ enddataset ]]
                         [[ dataset label:'{{ cyyrlinelbl }}' data:'{{ cyyrlinect | Join:',' }}' fillcolor:'{{ cpcolor }}' bordercolor:'{{ cpcolor }}' borderwidth:'3' pointradius:'4' pointbordercolor:'{{ cpcolor }}' pointcolor:'{{ cpcolor }}' pointhovercolor:'{{ cpcolor }}' ]] [[ enddataset ]]
                    {[ endchart ]}
                </div>
            </div>
            <hr style="height:1px; background-color:#000;">
    {% endfor%}
    {[ endpanel ]}
{% endif %}